Public Class WeeklyReportList
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblTotal As System.Web.UI.WebControls.Label
    Protected WithEvents lblTotalText As System.Web.UI.WebControls.Label
    Protected WithEvents PageSelect1 As SR.WebControls.PageSelect
    Protected WithEvents Pageselect2 As SR.WebControls.PageSelect
    Protected WithEvents dgReports As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lblCustomerReportList As System.Web.UI.WebControls.Label
    Protected WithEvents hrefCustomerReportList As System.Web.UI.HtmlControls.HtmlAnchor

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents Header1 As Header
    Private PAGE_SIZE As Integer = 20


    Protected ReadOnly Property CustomerID() As Integer
        Get
            Dim cid As Integer

            If Not Security.IsCustomer Then
                Try
                    cid = CInt(Request.QueryString("cid"))
                Catch ex As Exception
                    cid = -1
                End Try
            Else
                cid = Security.GetCurrentUser.UsIdPk
            End If

            If Not cid > 0 Then
                Throw New ApplicationException("Invalid or missing parameter")
            End If

            Return cid
        End Get
    End Property

    Protected Property WeeklyList() As DataView
        Get
            If IsNothing(Session("MIMS_WRS.Web.WeeklyReportList.WeeklyList")) Then
                Throw New ApplicationException("Session Expired")
            End If
            Return CType(Session("MIMS_WRS.Web.WeeklyReportList.WeeklyList"), DataView)
        End Get
        Set(ByVal Value As DataView)
            Session("MIMS_WRS.Web.WeeklyReportList.WeeklyList") = Value
        End Set
    End Property

    Private Function GetDataFromList() As DataTable
        Dim dt As DataTable
        dt = Me.WeeklyList.Table.Clone
        Dim Idx As Integer = Me.PageSelect1.CurrentPageIndex * Me.PageSelect1.PageSize
        Dim i As Integer = 0

        While (Idx + i < Me.WeeklyList.Count AndAlso i < 10)
            dt.ImportRow(Me.WeeklyList.Item(Idx + i).Row)
            i += 1
        End While

        Return dt
    End Function

    Private Sub BindGrid()
        Me.dgReports.DataSource = GetDataFromList()
        Me.dgReports.DataBind()

        Dim cnt As Integer = CInt(Me.WeeklyList.Count)

        Me.lblTotal.Text = cnt.ToString

        If cnt > 0 Then
            'create top page select
            Me.PageSelect1.ItemCount = cnt
            Me.PageSelect1.Create()

            'create bottom page select and sync with top page select
            Me.Pageselect2.ItemCount = Me.PageSelect1.ItemCount
            Me.Pageselect2.Create()
        Else
            Me.PageSelect1.Visible = False
            Me.Pageselect2.Visible = False
        End If

    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Page.IsPostBack Then
            Response.Cache.SetNoStore()

            If Security.IsCustomer Then
                Header1.MenuIndex = EMenuIndex.CustomerWeeklyReport
                hrefCustomerReportList.Visible = False
            Else
                lblCustomerReportList.Visible = False
                Header1.MenuIndex = EMenuIndex.InternalReport
                Header1.NavigationBar.Add("Back to Customer Reports List", AppRoot() & "/Internal/Reports.aspx")
            End If

            Dim ds1 As DataSet
            Dim ds2 As DataSet
            ds1 = COrderStatus.GetOrderStatusWeeklyList(Me.CustomerID)
            ds2 = CProductionStatus.GetProductionStatusWeeklyList(Me.CustomerID)

            Dim dcs1 As DataColumn() = {ds1.Tables(0).Columns(0), ds1.Tables(0).Columns(1)}
            ds1.Tables(0).PrimaryKey = dcs1

            Dim dcs2 As DataColumn() = {ds2.Tables(0).Columns(0), ds2.Tables(0).Columns(1)}
            ds2.Tables(0).PrimaryKey = dcs2

            ds1.Merge(ds2, False, MissingSchemaAction.Add)

            ds1.Tables(0).DefaultView.Sort = "YEAR DESC, WEEK DESC"

            Me.WeeklyList = ds1.Tables(0).DefaultView

            Me.PageSelect1.PageSize = Me.PAGE_SIZE
            Me.Pageselect2.PageSize = Me.PageSelect1.PageSize

            Me.lblTotalText.Text = "ITEM(S)"
            Me.BindGrid()
        End If
    End Sub

    Private Sub PageSelect1_PageIndexChanged(ByVal newPageIndex As Integer) Handles PageSelect1.PageIndexChanged
        Me.PageSelect1.CurrentPageIndex = newPageIndex
        Me.Pageselect2.CurrentPageIndex = newPageIndex
        Me.BindGrid()
    End Sub

    Private Sub Pageselect2_PageIndexChanged(ByVal newPageIndex As Integer) Handles Pageselect2.PageIndexChanged
        Me.PageSelect1.CurrentPageIndex = newPageIndex
        Me.Pageselect2.CurrentPageIndex = newPageIndex
        Me.BindGrid()
    End Sub

#Region "   Datagrid binding function"
    Protected Function GetProductionStatusLink(ByVal container As Object) As String
        Dim haslink As Boolean
        Dim w, y As Integer
        w = CInt(DataBinder.Eval(container, "DataItem.WEEK"))
        y = CInt(DataBinder.Eval(container, "DataItem.YEAR"))

        Try
            haslink = CBool(DataBinder.Eval(container, "DataItem.HasProductionStatus"))
        Catch ex As Exception
            haslink = False
        End Try

        If haslink Then
            Return String.Format("<a href='ProductionStatusReport.aspx?y={0}&w={1}&cid={2}'>View</a>", y, w, Me.CustomerID)
        Else
            Return "N/A"
        End If
    End Function

    Protected Function GetOrderStatusLink(ByVal container As Object) As String
        Dim haslink As Boolean
        Dim w, y As Integer
        w = CInt(DataBinder.Eval(container, "DataItem.WEEK"))
        y = CInt(DataBinder.Eval(container, "DataItem.YEAR"))

        Try
            haslink = CBool(DataBinder.Eval(container, "DataItem.HasOrderStatus"))
        Catch ex As Exception
            haslink = False
        End Try

        If haslink Then
            Return String.Format("<a href='OrderStatusReport.aspx?y={0}&w={1}&cid={2}'>View</a>", y, w, Me.CustomerID)
        Else
            Return "N/A"
        End If
    End Function
#End Region
End Class
